package com.zdsoft.blp.zongbuju.shouye.taoshihui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.entity.Address;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.entity.FenLei;

import java.util.List;

/**
 * Created by pc on 2016/9/9.
 */
public class AddressAdapter extends BaseAdapter {
    private static final int XUANZE =0;
    private static final int GUANLI =2;
    private List<Address> list;
    private Context mContext;
    private String addressId;
    private int code;
    private Integer index=null;


    public AddressAdapter(List<Address> list, Context mContext,int code, String addressId) {
        this.list = list;
        this.mContext = mContext;
        this.addressId = addressId;
        this.code = code;
    }

    public AddressAdapter(List<Address> list, Context mContext, int code) {
        this.list = list;
        this.mContext = mContext;
        this.code = code;
    }

    public void change(int selected){
        index =selected;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.addressitem, null);
            holder.iv = (ImageView) convertView.findViewById(R.id.iv);
            holder.tv_moren = (TextView) convertView.findViewById(R.id.tv_moren);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            holder.tv_address = (TextView) convertView.findViewById(R.id.tv_address);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Address address = list.get(position);
        holder.tv_name.setText(address.getName());
        holder.tv_phone.setText(address.getDianhua());
        holder.tv_address.setText(address.getXiangxi());
        if ("Y".equals(address.getMoren())){
            holder.tv_moren.setVisibility(View.VISIBLE);
        }
        if(code==XUANZE){
            if(index==null){
                if(addressId.equals(address.getId())){
                    holder.iv.setImageResource(R.drawable.wszf);
                }else {
                    holder.iv.setImageResource(R.drawable.xxzf);
                }
            }else if(index==position){
                holder.iv.setImageResource(R.drawable.wszf);
            }else {
                holder.iv.setImageResource(R.drawable.xxzf);
            }
        }else if(code ==GUANLI){
            holder.iv.setImageResource(R.drawable.qrzf_jt);
        }

        return convertView;
    }

    class ViewHolder {
        ImageView iv;
        TextView tv_moren;
        TextView tv_name;
        TextView tv_phone;
        TextView tv_address;
    }
}
